﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraBars;
using DevExpress.XtraEditors.Controls;
using KQReport.Utils;

namespace KQReport
{
    public partial class Uc1 : UserControl
    {
        private List<Depart> _departs;

        public Uc1()
        {
            InitializeComponent();
        }

        private void Uc1_Load(object sender, EventArgs e)
        {
            groupControl1.Text = Conf.Analyse1.Value;
            _departs = Depart.LoadDeparts();
            foreach (Depart depart in _departs)
                FillDepart(depart);
            if (cobDeparts.Properties.Items.Count > 0)
                cobDeparts.SelectedIndex = 0;
            Dictionary<string, string> dics = User.LoadAllSmallUsers();
            foreach (var pair in dics)
            {
                string display = string.Format("{0}({1})", pair.Value, pair.Key);
                cobUsers.Properties.Items.Add(pair.Key, display); //,CheckState.Checked,true);
            }
            dateEdit2.DateTime = DateTime.Now.Date;
            dateEdit1.DateTime = DateTime.Now.Date.AddMonths(-1);
        }

        private void FillDepart(Depart depart)
        {
            cobDeparts.Properties.Items.Add(depart.Display);
            foreach (Depart child in depart.Departs)
                FillDepart(child);
        }

        private void simpleButton1_Click(object sender, EventArgs e)
        {
            string user = null;
            if (cobUsers.EditValue != null)
            {
                string str = cobUsers.EditValue.ToString();
                if (!string.IsNullOrEmpty(str))
                {
                    bool allchecked = true;
                    foreach (CheckedListBoxItem item in cobUsers.Properties.Items)
                        if (item.CheckState != CheckState.Checked)
                        {
                            allchecked = false;
                            break;
                        }
                    if (!allchecked)
                    {
                        string[] array = str.ListFromDisplay();
                        user = array.Select(p => string.Format("'{0}'", p.Trim())).ListGetDisplay();
                    }
                }
            }

            if (cobDeparts.SelectedIndex == -1)
                throw new Exception("未设置部门！");
            Depart depart = _departs[cobDeparts.SelectedIndex];
            DateTime start = dateEdit1.DateTime;
            DateTime end = dateEdit2.DateTime;
            if (start > end)
                throw new Exception("起始时间就小于结束时间！");
            end = end.AddDays(1);
            List<User> users = User.LoadUsers(depart.Id, user);
            List<CheckIn> checkins = CheckIn.GetCheckDics(depart.Id, user, start, end);
            gridControl1.DataSource = KqDetail.MergeResult(users, checkins, ck0.Checked, ck1.Checked, ck2.Checked);
            gridView.BestFitColumns();
        }

        private void barButtonItem1_ItemClick(object sender, ItemClickEventArgs e)
        {
            string file = string.Format(@"{0}\{1}.xls", Path.GetTempFileName().Replace(".tmp", string.Empty),
                Conf.Analyse1.Value);
            ExportHelper.ExportToEx(gridView, file, "*.xls", this);
        }

        private void barButtonItem2_ItemClick(object sender, ItemClickEventArgs e)
        {
            string file = string.Format(@"{0}\{1}.xlsx", Path.GetTempFileName().Replace(".tmp", string.Empty),
                Conf.Analyse1.Value);
            ExportHelper.ExportToEx(gridView, file, "*.xlsx", this);
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            string file = string.Format(@"{0}\{1}.xls", Path.GetTempFileName().Replace(".tmp", string.Empty),
                Conf.Analyse1.Value);
            ExportHelper.ExportToEx(gridView, file, "*.xls", this);
        }
    }
}